<template>
	<view class="page-fade-in" :style="{opacity: pageOpc}">
		<template v-if="pageOpc">
			<view class="content">
				<view class="top-box flexWrap">
					<view class="screen-box">
						<view class="date-box flexWrapNo" @tap="showDate()">
							<image src="/static/index/calendar.png" class="calendar-icon"></image>
							{{starTime}}～{{endTime}}
						</view>
						<view class="date-boxs" v-if="dateFlag">
							<view class="date-title">时间筛选</view>
							<view class="date-cont flexWrap">
								<view class="screen-item flexWrap">
									<picker mode="date" :data-index='0' :value="starTime" @change="bindDateChange" :end="date">
										<view class="select-input" v-if="starTime">{{starTime}}</view>
										<view class="select-input" v-else>请选择开始时间</view>
									</picker>
									<text class="triangle"></text>
								</view>
								<view class="screen-item flexWrap">
									<picker mode="date" :data-index='1' :value="date" @change="bindDateChange" :start="starTime"
										:end="date">
										<view class="select-input" v-if="endTime">{{endTime}}</view>
										<view class="select-input" v-else>请选择结束时间</view>
									</picker>
									<text class="triangle"></text>
								</view>
							</view>
							<view class="determine-btn flexWrapNo">
								<!-- <view @tap="reSet()">重置</view> -->
								<view class="determine" @tap="submit()">确认</view>
							</view>
						</view>
						<view class="mask-wrap" v-if="dateFlag" @tap="closeDate()"></view>
						<view class="nav-box flexWrapNo">
							<!-- <view class="nav-item" :class="{active:currmentIndex==0}" @tap="changeNav(0)">时长</view> -->
							<view class="nav-item" :class="{active:currmentIndex==1}" @tap="changeNav(1)">消费</view>
							<view class="nav-item" :class="{active:currmentIndex==2}" @tap="changeNav(2)">充值</view>
							<view class="nav-item" :class="{active:currmentIndex==3}" @tap="changeNav(3)">次数</view>
						</view>
					</view>
					<view class="memberData-img">
						<image src="/static/index/menberData.png"></image>
					</view>
				</view>
				<view class="total-box flexWrap">
					<view class="total-num" v-if="currmentIndex==0">时长top100</view>
					<view class="total-num" v-if="currmentIndex==1">消费top100</view>
					<view class="total-num" v-if="currmentIndex==2">充值top100</view>
					<view class="total-num" v-if="currmentIndex==3">次数top100</view>
				</view>
				<scroll-view class="VerticalMain" lower-threshold="50rpx" @scrolltolower="lower" scroll-y
				scroll-with-animation style="height: calc(70vh);" >
					<view class="data-box">
						<view class="data-item" v-for="(item,index) in dataList" :key="index">
							<view class="data-info flexWrap">
								<view class="flexWrapNo">用户 : <text class="nickname">{{item.nickname}}</text>  {{item.mobile}}  <text style="margin-left: 30upx;color: #EEC162;" @tap="copy(item.mobile)">复制</text></view>
								<text v-if="currmentIndex==0">{{item.quantity}}小时</text>
								<text v-if="currmentIndex==1">{{item.actual_payment}}</text>
								<text v-if="currmentIndex==2">{{item.recharge_money}}</text>
								<text v-if="currmentIndex==3">{{item.order_sum}}</text>
							</view>
							<view class="progress-box">
								<view class="progress-child" :style="{width:item.progress}"></view>
							</view>
						</view>
						<view class="empty-box" v-if="showEmpty">
							<image src="https://static.mo-yu.vip/wechat/images/orderEmpty.png"></image>
						</view>
						<uni-load-more v-if="showLoadStatus" :status="status" :size="14" :content-text="contentText" />
					</view>
				</scroll-view>
			</view>
		</template>
	</view>
</template>

<script>
	import uniLoadMore from '@/components/uni-load-more/uni-load-more.vue'
	export default {
		components: {
			uniLoadMore
		},
		data() {
			const currentDate = this.getDate({
				format: true
			})
			const starDate = this.getDate('start')
			return {
				pageOpc:0,
				currmentIndex:1,
				//下拉刷新
				page: 1,
				size:15,
				showEmpty: true,
				showLoadStatus: false,
				more: true,
				contentText: {
					contentdown: '上拉加载更多~',
					contentrefresh: '加载中...',
					contentnomore: '已经到底了~~'
				},
				status: 'loading',
				sum:"0.00",
				dataList:[],
				starTime: starDate,
				endTime: currentDate,
				date: currentDate,
				dateFlag:false
			}
		},
		onLoad() {
			this.getData()
		},
		onReady(){
			setTimeout(() => {
				this.pageOpc=1
			}, 100)
		},
		// 下拉刷新
		onPullDownRefresh: function() {
			this.freshList();
			this.$tools.loadIng()
			setTimeout(() => {
				uni.stopPullDownRefresh()
			}, 500)
		},
		methods: {
			copy(mobile){
				let that = this
				uni.setClipboardData({
					data:mobile,
					success: function() {
						console.log('success');
					}
				});
			},
			// 触底刷新
			lower() {
				if (this.more) {
					this.getData()
				}
			},
			// 获取时间
			getDate(type) {
				const date = new Date();
				date.setDate(date.getDate() - 1);
				if (type === 'start') {
					date.setDate(date.getDate() - 6);
				}
				let year = date.getFullYear();
				let month = date.getMonth() + 1;
				let day = date.getDate();
				month = month > 9 ? month : '0' + month;
				day = day > 9 ? day : '0' + day;
				return `${year}-${month}-${day}`;
			},
			// 选择时间
			bindDateChange(e) {
				let index = e.currentTarget.dataset.index
				if (index == 0) {
					this.starTime = e.target.value
				} else {
					this.endTime = e.target.value
				}
			},
			// 重置
			reSet() {
				this.starTime = this.getDate('start')
				this.endTime = this.getDate({
					format: true
				})
			},
			// 提交
			submit() {
				this.freshList();
				this.closeDate();
			},
			showDate(){
				this.dateFlag=!this.dateFlag
			},
			closeDate(){
				this.dateFlag=false
			},
			changeNav(index){
				this.currmentIndex=index
				this.freshList();
			},
			// 刷新列表
			freshList() {
				this.page = 1
				this.more = true
				this.status = 'loading'
				this.dataList = []
				this.getData();
			},
			getData(){
				let self=this
				this.$request.post(this.$api.memberData, {
					store_id:uni.getStorageSync('store_id'),
					type:Number(this.currmentIndex),
					start_time:this.starTime,
					end_time:this.endTime,
					page:this.page++,
					num:this.size
				}, {
					token:true
				}).then(res => {
					if (res.code == 1) {
						let {
							data = {}
						} = res;
						let newArray =data.list
						this.sum=data.count
						for (let i in newArray) {
							if(this.currmentIndex==1){
								newArray[i].progress = Number(newArray[i].actual_payment)/this.sum*100+'%'
								
							}else if(this.currmentIndex==2){
								newArray[i].progress = Number(newArray[i].recharge_money)/this.sum*100+'%'
							}else{
								newArray[i].progress = Number(newArray[i].order_sum)/this.sum*100+'%'
							}
						}
						if (newArray.length == 0 && self.page == 2) {
							self.showEmpty = true
							self.showLoadStatus = false
							self.more = false // 不能再次请求了
							return false;
						} else {
							self.showLoadStatus = true
							self.more = true
							self.showEmpty = false
							self.status = 'loading'
						}
						if (newArray.length < this.size) { // 说明没有更多数据
							self.status = 'noMore'
							self.more = false
							self.showEmpty = false
						}
						self.dataList=self.dataList.concat(newArray);
					}
				}).finally(() => {
					setTimeout(()=>{
						this.$tools.hideLoading()
					}, 1000)
				})
			},
		}
	}
</script>

<style lang="scss">
	.content{
		padding: 0 36upx;
	}
	.VerticalMain {
		flex: auto;
		padding-top: 10rpx;
	}
	.top-box{
		.screen-box{
			.date-box{
				font-size: 28upx;
				font-family: PingFang SC;
				font-weight: 500;
				line-height: 60upx;
				color: #333;
				.calendar-icon{
					width: 48upx;
					height: 48upx;
					margin-right: 24upx;
				}
			}
			.nav-box{
				margin-top: 32upx;
				.nav-item{
					width: 120upx;
					height: 60upx;
					line-height: 60upx;
					text-align: center;
					background:#fff;
					border-radius: 16upx;
					font-size: 32upx;
					font-family: PingFang SC;
					font-weight: 500;
					color: #333;
					margin-right: 20upx;
				}
				.active{
					background-color: #EEC162;
				}
			}
		}
		.memberData-img{
			width: 228upx;
			height: 228upx;
			flex: none;
		}
	}
	.total-box{
		font-size: 24upx;
		font-family: PingFang SC;
		font-weight: 400;
		line-height: 34upx;
		color: #6E6E6E;
		border-top: 2upx solid #E3E3E3;
		border-bottom: 2upx solid #E3E3E3;
		padding: 44upx 0 42upx 0;
		justify-content: center;
		.total-num{
			font-size: 48upx;
			font-family: PingFang SC;
			font-weight: 500;
			line-height: 66upx;
			color: #E69C2F;
		}
	}
	.data-box{
		margin-top: 40upx;
		.data-item{
			margin-top: 20upx;
			.data-info{
				font-size: 24upx;
				font-family: PingFang SC;
				font-weight: 400;
				line-height: 60upx;
				color: #6E6E6E;
				.nickname{
					max-width: 200upx;
					@include ellipsis(1);
				}
			}
			.progress-box{
				height: 20upx;
				background:#969696;
				border-radius: 20upx;
				.progress-child{
					height: 20upx;
					background:#EEC162;
					border-radius: 20upx;
				}
			}
		}
	}
	.mask-wrap {
		position: fixed;
		top: 0;
		bottom: 0;
		left: 0;
		right: 0;
		margin: auto;
		z-index: 99;
		background: rgba(0, 0, 0, .4);
	}
	.empty-box{
		width: 476upx;
		height: 280upx;
		margin:250upx auto;
	}
</style>
